<?php
$file = "fifo_x";
if (!posix_access($file,POSIX_F_OK)){
    if (posix_mkfifo($file,0666)){
        fprintf(STDOUT,"create ok\n");
    }
}

pcntl_signal(SIGPIPE,function ($signo){
    fprintf(STDOUT,"signo=%d\n",$signo);
});
$pid = pcntl_fork();
if ($pid==0){
   $fd =  fopen($file,"r+");
   stream_set_blocking($fd,0);
   $i=0;
   while (1){
      $data = fread($fd,5);
      if ($data){
       $i++;
       fprintf(STDOUT,"read process pid=%d,recv:%s\n",getmypid(),$data);
       if ($i==5){
           fclose($fd);
           break;
       }
      }
   }
   exit(0);
}

$fd = fopen($file,"w+");
stream_set_blocking($fd,0);
while (1){
    pcntl_signal_dispatch();
    $len = fwrite($fd,"hello",5);
    $pid = pcntl_wait($status,WNOHANG);
    if ($pid>0){
        fprintf(STDOUT,"exit pid=%d\n",$pid);
    }
}
fclose($fd);
